<template>
  <div class='container'>
    <el-card class="box-card">
      <!-- 顶部搜索栏 -->
      <el-row >
        <el-col :span="18">
          <el-form ref="form"  :inline="true" :model="form" label-width="80px" label-position='right'>
            <el-form-item label="关键字">
              <el-input v-model.trim="form.keyword"  placeholder="根据文章标题搜索"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="form.state" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button  @click="resetForm('numberValidateForm')">清除</el-button>
              <el-button type="primary" @click="loadList">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" class="success-button">
        <el-button type="success" icon="el-icon-edit" @click="onAddArticle" >新增技巧</el-button>
        </el-col>
      </el-row>
      <!-- 提示文案 -->
      <el-alert
        type="info"
        show-icon>
        <template slot="title">
          数据一共{{total}} 条
        </template>
      </el-alert>
      <!-- 表格 -->
      <el-table
        :data="tableData"
        v-loading="loading"
        style="width: 100; margin: 10px 0">
        <el-table-column
         type="index"
          label="序号"
          width="80">
        </el-table-column>
        <el-table-column
          prop="title"
          label="文章标题"
          width="400">
          <template slot-scope="{row}">
            {{row.title}}  <i v-if="row.videoURL!==(''&&null)" class="el-icon-film" style="color:blue" @click="showVideo(row)"></i>
          </template>
        </el-table-column>
        <el-table-column
          prop="visits"
          label="阅读数">
        </el-table-column>
        <el-table-column
          prop="date"
          label="录入人">
          <template>
            {{$store.getters.name}}
          </template>
        </el-table-column>
        <el-table-column
          label="录入时间">
          <template slot-scope="{row}">
            {{row.createTime | date}}
          </template>
        </el-table-column>
        <el-table-column
          label="状态"
          width="100">
          <template slot-scope="{row}">
            {{row.state===1?'已启用':'已禁用'}}
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          width="180"
          label="操作">
          <template slot-scope="{row}">
            <el-button type="text" @click="onPreview(row)">预览</el-button>
            <el-button type="text" @click="changeState(row)">{{row.state===1?'启用':'禁用'}}</el-button>
            <el-button type="text" :disabled="row.state===1?true:false"  @click="onEdit(row)">修改</el-button>
            <el-button type="text" :disabled="row.state===1?true:false"  @click="onDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <page-nation :total='total' :formlist='formList' @on-success='onChangePage'/>
      <!-- 预览 -->
      <preview :previewDialogVisible.sync='previewDialogVisible' :rows='row' />
      <!-- 新增 -->
      <articles-add :dialogVisible.sync="addDialogVisible" v-if="addDialogVisible" @success='loadList' :rows="row"/>
      <!-- 视频 -->
      <Video :videoURL="rowUrl"  :videoDialogVisible.sync='videoDialog' v-if="videoDialog" />
    </el-card>
  </div>
</template>

<script>
import PageNation from '../components/PageNation.vue'
import { list, remove, changeState } from '@/api/hmmm/articles.js'
import dayjs from 'dayjs'
import Preview from '../components/articles/preview.vue'
import ArticlesAdd from '../components/articles-add.vue'
import Video from '../components/articles/video.vue'
export default {
  components: { PageNation, Preview, ArticlesAdd, Video },
  data () {
    return {
      form: {
        keyword: '',
        state: ''
      },
      loading: true,
      tableData: [],
      formList: {
        page: 1,
        pagesize: 10 // 页尺寸
      },
      total: 1, // 共几页,
      previewDialogVisible: false, // 预览
      row: {},
      options: [
        {
          value: '1',
          label: '启用'
        },
        {
          value: '0',
          label: '禁用'
        }
      ],
      addDialogVisible: false, // 添加
      videoDialog: false, // 视频
      rowUrl: ''
    }
  },
  created () {
    this.loadList()
  },
  methods: {
    // 加载数据
    async loadList () {
      this.loading = true
      const obj = { ...this.formList }
      if (this.form.keyword !== '') {
        obj.keyword = this.form.keyword
      }
      if (this.form.state !== '') {
        obj.state = this.form.state
      }
      const res = await list(obj)
      this.loading = false
      this.tableData = res.data.items
      this.total = res.data.counts
      this.row = {}
    },
    // 更改页数
    async onChangePage (val) {
      this.formList.page = val.pagenum
      this.formList.pagesize = val.pagesize
      this.loadList()
    },
    resetForm () {
      console.log(1111)
      this.$refs.form.resetFields()
      this.form = {
        keyword: '',
        type: ''
      }
    },
    // 预览
    onPreview (row) {
      this.row = row
      this.previewDialogVisible = true
    },
    // 添加 文章
    onAddArticle () {
      this.row = {}
      this.addDialogVisible = true
    },
    // 编辑
    onEdit (row) {
      this.addDialogVisible = true
      this.row = row
    },
    // 删除
    async onDelete (row) {
      await remove({
        id: row.id
      })
      this.loadList()
    },
    // 状态修改
    async changeState (row) {
      row.state = row.state === 1 ? 0 : 1
      await changeState({
        id: row.id,
        state: row.state
      })
      this.$message.success('操作成功')
    },
    // 视频
    showVideo (row) {
      this.videoDialog = true
      this.rowUrl = row.videoURL
    }
  },
  filters: {
    date (val) {
      return dayjs(dayjs(val).$d).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style scoped lang='less'>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}

.box-card {
  // width: 100%;
  margin: 10px;
  // box-sizing: border-box;
}
.success-button {
  text-align: right;
}
</style>
